<?php
/**
 * @File      : Base_Controller.php
 * @Project      : thokim
 * @author      : ThanhMinh
 * @email      : thanhminhld@yahoo.com - thanhminhld@gmail.com
 * @copyright : 2009
 */

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Base_Controller extends Controller
{
    protected $lang_default = null;
    protected $lang_tag     = null;
    /**
     * Base_Controller constructor init Smarty and get config
     * @access public
     *
     */
    public function __construct()
    {
        parent::Controller();

        $config = &get_config();
        $langDefault         = $this->lang_model->getLangDefault();
        $this->lang_default  = $langDefault->id;
        $lang_tag            = array('lang_tag' => $langDefault->tag);
        $this->lang_tag      = $langDefault->tag;
        $this->session->set_userdata($lang_tag);
        $this->assignMenu($config);
    }
    /**
     * assignMenu
     * @access private
     * @param
     * @return
     */
    private function assignMenu($config)
    {
        /**
         * Your code here
         */
        $controlname = $this->uri->segment(1);
        $arrMenuCss = array('introduction' => 'introcss',
                            'services'     => 'servicescss',
                            'products'     => 'productcss',
                            'news'         => 'newscss',
                            'consultant'   => 'consultantcss',
                            'sitemap'      => 'sitemapcss',
                            'contact'      => 'contactcss');
        if ($controlname == '') {
            $controlname = 'home';
        }
        if  (array_key_exists($controlname, $arrMenuCss)) {
            $this->msmarty->assign($arrMenuCss[$controlname], 'current-menu-item current_page_item');
        } else {
            $this->msmarty->assign('homecss', 'current-menu-item current_page_item');
        }

        // home page
        $this->msmarty->assign('home_link',     $config['base_url']);
        // Introduction
        $this->msmarty->assign('intro_link',    $config['base_url'] . 'introduction/');
        // services
        $this->msmarty->assign('service_link',  $config['base_url'] . 'services/');
        // products
        $this->msmarty->assign('products_link', $config['base_url'] . 'products/');
        // news
        $this->msmarty->assign('newss_link',    $config['base_url'] . 'news/');
        // consultant
        $this->msmarty->assign('consultant_link', $config['base_url'] . 'consultant/');
        // sitemap
        $this->msmarty->assign('sitemap_link',  $config['base_url'] . 'sitemap/');
        // Contact
        $this->msmarty->assign('contact_link',  $config['base_url'] . 'contact/');

        $this->loadMenuCategory($config);
        // header Banner
        $this->loadHeaderBanner();
        if ($controlname != 'sitemap') {
            /**
             * Left colume
             */
            // left menu
            $this->loadLeftMenu($config);
            // featured news
            $this->loadFeaturedNews();
            /**
             * Right colume
             */
            // focus project
            $this->loadFocusProject();
            // web link
            $this->loadWeblink();
            // advertisment
            $this->loadAds();
            // focusService
            $this->loadFocusService();
        }
    }
    /**
     * @function    loadMenuCategory
     *
     * @param       array $config
     */
    private function loadMenuCategory ($config)
    {
        $arrWhere = array('lang_id' => $this->lang_default);
        $arrModel = array('services_model'   => array('parentUrl'      => $config['base_url']
                                                                          . 'services/services-category/SC-',
                                                      'childUrl'       => $config['base_url'] . 'services/'
                                                                          . 'services-category/SC-',
                                                      'parentCss'      => 'class="sub-menu"',
                                                      'childCss'       => 'id="menu-item" class="menu-item"',
                                                      'assignMenuList' => 'serviceMenuList'),

                          'products_model'   => array('parentUrl'      => $config['base_url'] . 'products/'
                                                                          . 'products-category/PC-',
                                                      'childUrl'       => $config['base_url'] . 'products/'
                                                                          . 'products-category/PC-',
                                                      'parentCss'      => 'class="sub-menu"',
                                                      'childCss'       => 'id="menu-item" class="menu-item"',
                                                      'assignMenuList' => 'productMenuList'),

                          'news_model'       => array('parentUrl'      => $config['base_url'] . 'news/'
                                                                          . 'news-category/NC-',
                                                      'childUrl'       => $config['base_url'] . 'news/'
                                                                          . 'news-category/NC-',
                                                      'parentCss'      => 'class="sub-menu"',
                                                      'childCss'       => 'id="menu-item" class="menu-item"',
                                                      'assignMenuList' => 'newsMenuList'),

                          'consultant_model' => array('parentUrl'      => $config['base_url'] . 'consultant/'
                                                                          . 'consultant-category/CC-',
                                                      'childUrl'       => $config['base_url'] . 'consultant/'
                                                                          . 'consultant-category/CC-',
                                                      'parentCss'      => 'class="sub-menu"',
                                                      'childCss'       => 'id="menu-item" class="menu-item"',
                                                      'assignMenuList' => 'consultantMenuList')
                          );
        foreach ($arrModel as $model => $url) {
            $arrCategory = $this->$model->loadList($arrWhere, 'all', 0, 'category_id', 'ASC');
            $arrCategory = array2ParentChildStructor($arrCategory, 'parent_id');
            $strChild = menuRecursive($arrCategory, 0, $url['parentUrl'], $url['childUrl'],
                                      $url['parentCss'], $url['childCss']);
            $this->msmarty->assign($url['assignMenuList'], $strChild);
        }
    }
    /**
     * @function    loadHeaderBanner
     *
     *
     *
     */
    private function loadHeaderBanner()
    {
        $arrWhere = array('banner_show' => 'Y');
        $bannerList = $this->banner_model->loadList($arrWhere, 'all', 0);
        $this->msmarty->assign('controlImgPath', 'application/uploads/banner/');
        $this->msmarty->assign('bannerList', $bannerList);
    }
    /**
     * @function    loadLeftMenu
     *
     */
    private function loadLeftMenu($config)
    {
        $arrWhere = array('lang_id' => $this->lang_default);
        $arrModel = array(
                          'products_model'   => array('parentUrl'      => $config['base_url'] . 'products/'
                                                                          . 'products-category/PC-',
                                                      'childUrl'       => $config['base_url'] . 'products/'
                                                                          . 'products-category/PC-',
                                                      'parentCss'      => 'class="sub-menu"',
                                                      'childCss'       => 'id="menu-item" class="menu-item"',
                                                      'assignMenuList' => 'leftMenuList')
                          );
        foreach ($arrModel as $model => $url) {
            $arrCategory = $this->$model->loadList($arrWhere, 'all', 0, 'category_id', 'ASC');
            $arrCategory = array2ParentChildStructor($arrCategory, 'parent_id');
//print_r($arrCategory[0]);
            $run = 0;
            $strChild = leftMenuRecursive($arrCategory, 0, $run, $url['parentUrl'], $url['childUrl'],
                                          $url['parentCss'], $url['childCss']);
            $this->msmarty->assign($url['assignMenuList'], $strChild);
        }
    }
    /**
     * @function    loadFeaturedNews
     *
     * Lay tin noi bat
     */
    private function loadFeaturedNews()
    {
        /**
         * Tin tuc noi bat - Tin kien truc
         */
        $arrWhere = array('lang_id'       => $this->lang_default,
                          'news_featured' => 'Y',
                          'news_show'     => 'Y');
        $featureNews = $this->newspost_model->loadList($arrWhere, 2, 0, 'news_id', 'DESC');
        $this->msmarty->assign('featureNews', $featureNews);
        $this->msmarty->assign('featureNewsUrl', 'news/news-detail/NP-');
    }
    /**
     * @function    loadFocusProject
     *
     * Du an tieu bieu
     */
    private function loadFocusProject()
    {
        $arrWhere = array('lang_id'       => $this->lang_default,
                          'product_focus' => 'Y',
                          'product_show'  => 'Y');
        $arrProductList = $this->productspost_model->loadList($arrWhere, 1, 0, 'product_id', 'DESC');
        $arrFocusProd = array();
        if (count($arrProductList) > 0) {
            $arrFocusProd[] = $arrProductList[0];
        }
        $this->msmarty->assign('arrFocusProd', $arrFocusProd);
        $this->msmarty->assign('forcusProdUrl', 'products/products-detail/PP-');
    }
    /**
     * @function loadWeblink
     *
     */
    private function loadWeblink()
    {
        /**
         * Lien ket website
         */
        $arrWhere = array('lang_id' => $this->lang_default);
        $arrWeblink = $this->weblink_model->loadList($arrWhere, 'all', 0, 'web_id', 'DESC');
        $this->msmarty->assign('arrWeblink', $arrWeblink);
    }
    /**
     * @function loadAds
     *
     */
    private function loadAds()
    {
        /**
         * Quang cao
         */
        $arrWhere = array('adv_show' => 'Y');
        $arrAds = $this->advertisement_model->loadList($arrWhere, 'all', 0);
        $this->msmarty->assign('arrAds', $arrAds);
    }
    /**
     * @function loadFocusService
     *
     */
    private function loadFocusService()
    {
        /**
         * Dich vu tieu bieu
         */
        $arrWhere = array('lang_id'       => $this->lang_default,
                          'service_focus' => 'Y',
                          'service_show'  => 'Y');
        $arrFocusService = $this->servicespost_model->loadList($arrWhere, 'all', 0, 'service_id', 'DESC');
        $this->msmarty->assign('arrFocusService', $arrFocusService);
        $this->msmarty->assign('focusServiceUrl', 'services/services-detail/SP-');
    }
}

?>